package com.shuyihui.databindinglib;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;

public abstract class BaseActivity extends AppCompatActivity {
    public final String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    private ProgressDialog mProgressDialog;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView();
        mContext = this;
        initView(savedInstanceState);
    }

    protected void initContentView() {
        setContentView(getContentLayout());
    }


    /**
     * @return 布局文件
     */
    protected abstract int getContentLayout();

    /**
     * 设置view
     * @param savedInstanceState
     */
    protected abstract void initView(Bundle savedInstanceState);

    public void setToolBar(Toolbar toolbar) {
        mToolbar = toolbar;
        setSupportActionBar(mToolbar);
        //设置actionBar的标题是否显示，对应ActionBar.DISPLAY_SHOW_TITLE。
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        setBackArrow();
    }

    protected void setTitleString(String titles) {
        if (mToolbar != null) {
            mToolbar.setTitle(titles);
        }
    }

    protected void setTitleString(CharSequence title) {
        if (mToolbar != null) {
            mToolbar.setTitle(title);
        }
    }

    @Override
    public void finish() {
        closeKeyboard();
        super.finish();
    }

    public void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context
                    .INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = res.getConfiguration();
        /*1 设置正常字体大小的倍数*/
        config.fontScale = 1.0f;
        createConfigurationContext(config);
        return res;
    }



    /**
     * 设置左上角back按钮
     */
    public void setBackArrow() {
        //TODO toolber 左边返回
//        setBackArrow(R.drawable.ic_baseline_arrow_back);
    }

    /**
     * 设置左上角back按钮
     */
    public void setBackArrow(int drawable) {
        final Drawable upArrow = ContextCompat.getDrawable(mContext,drawable);
        //给ToolBar设置左侧的图标
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
        // 给左上角图标的左边加上一个返回的图标 。对应ActionBar.DISPLAY_HOME_AS_UP
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回按钮的点击事件
        setNavigationOnClickListener();
    }

    protected void setNavigationOnClickListener() {
        if (mToolbar == null) {
            return;
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyLeftClick(false);
            }
        });
    }

    protected void MyLeftClick(boolean b) {
        finish();
    }



}

